<template>
	<view class="goods-box">
		<scroll-view scroll-y style="height: 100%;">
			<view v-for="(item,index) in goodsList">
				<uni-row class="row">
					<uni-col :span="9">
						<view @click="clickGoods(item._id)">
							<image class="goods-img" :src="item.goods_banner_imgs[0].url"></image>
						</view>
					</uni-col>
					<uni-col :span="15">
						<view class="goods-desc-box" @click="clickGoods(item._id)">
							<p>
								{{item.name}}
							</p>
							<text>
								{{item.goods_desc}}
							</text>
						</view>
						<view class="price-box">
							<text class="price-font">
								<text class="yuan-font">￥</text>
								{{convert(item.goods_price).integer}}.
								<text class="price-font" style="font-size: 32rpx;">
									{{convert(item.goods_price).point}}
								</text>
							</text>
						</view>
						<uni-icons type="cart" size="30" class="addCart" @click="addShoppingCart(item._id)"
							color="#FF5000"></uni-icons>
					</uni-col>
				</uni-row>
				<view style="height: 10px;"></view>
			</view>
			<view style="height: 30px;"></view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: "goodsList",
		data() {
			return {
				goodsList: [],
				lastDate: null,
			};
		},
		onReachBottom() {
			console.log('触底了');
		},
		methods: {
			convert(money) {
				return {
					integer: Math.floor(money / 100),
					point: money % 100
				}
			},
			async clickGoods(id) {
				let res = await uniCloud.importObject('getDataBaseData').getById(id)
				uni.navigateTo({
					url: '/pages/details/details?item=' + encodeURIComponent(JSON.stringify(res[0]))
				})
			},
			async getGoods(lastDate) {
				let res = await uniCloud.importObject('goods').getAllGoods(this.lastDate)
				if (res.length == 0) {
					uni.showToast({
						title: '没有数据了',
						icon: "loading",
						duration: 800
					})
					return;
				}
				this.lastDate = res[res.length - 1].auto_createdate

				this.goodsList = [...this.goodsList, ...res]
				this.$emit('sendLastDate', this.lastDate + '')
			},
			//添加购物车
			addShoppingCart(id) {
				const db = uniCloud.database();
				db.collection('shop-cart').add({
					'goods_id': id
				}).then(res => {
					uni.showToast({
						title: '添加成功！',
						icon: 'success',
						duration: 800
					})
				}).catch(e => {
					uni.showToast({
						title: '不能重复添加！',
						icon: 'error',
						duration: 800
					})
				})
			},
		}
	}
</script>

<style>
	.goods-box {
		width: 710rpx;
		margin: 10px auto 0rpx;
	}

	.goods-desc-box {
		height: 70%;
		width: 85%;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.goods-desc-box text {
		font-size: 12px;
		color: #a2a2a2;
	}

	.goods-img {
		height: 200rpx;
		width: 200rpx;
		border-radius: 8px
	}

	.row {
		height: 200rpx;
		border-radius: 18rpx;
		background-color: #ffffff;
	}

	.price-font {
		font-size: 40rpx;
		color: #FF5000;
		font-family: Verdana;
	}

	.title-font {
		font-size: Tahoma;
		font-weight: bold;
	}

	.yuan-font {
		color: #FF5000;
		font-size: 32rpx;
	}

	.price-box {
		height: 30%;
		width: 85%;
		word-wrap: break-word;
		position: absolute;
		bottom: 0rpx;
	}



	.x1 {
		font-size: 32rpx;
		margin: 10rpx;
		width: 50rpx;
		height: 50rpx;
		border: #8a8a8a 1px solid;
		border-radius: 18rpx;
		display: inline-block;
		line-height: 50rpx;
		text-align: center;
	}

	.addCart {
		height: 60rpx;
		width: 60rpx;
		position: absolute;
		bottom: 10rpx;
		right: 10rpx;
	}
</style>